import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const state = {
    carStore: JSON.parse(sessionStorage.getItem("carStore")) || {}, //汽车临时变量
    carStorage: JSON.parse(localStorage.getItem("carStorage")) || {}, //汽车永久存储
}

// 定义 getters
var getters = {
    trainStore(state) {
        return state.carStore
    },
    trainStorage(state) {
        return state.carStorage
    },
}

const actions = {
    carStore({ commit, state }) {
        commit("carStore");
    },
    carStorage({ commit, state }) {
        commit("carStorage");
    },
}

const mutations = {
    // 此处的事件为上方 actions 中的 commit(types.TPEOPLE)
    carStore(state, data) {
        sessionStorage.setItem("carStore", JSON.stringify(data));
        state.carStore = data;
    },
    carStorage(state, data) {
        localStorage.setItem("carStorage", JSON.stringify(data));
        state.carStorage = data;
    },
}

var car = {
    state,
    getters,
    actions,
    mutations
}

export default new Vuex.Store({
    modules: { car },
});
